//
//  Router.swift
//  DigitalMuseum
//
//  Created by cheyongzi on 2023/12/19.
//

import UIKit

@objc enum RouterStyle: Int {
    case Push,Present
}

@objc protocol RouterProtocol{
    static func routerPage(input: Any?)->UIViewController?
    
    @objc optional static func routerStyle()->RouterStyle
}

class Router{
    static func routerClass(_ pageClass: AnyClass,input: Any? = nil){
        guard let controller = pageClass.routerPage?(input: input) else {
            return
        }
        let routerStyle = pageClass.routerStyle?() ?? .Push
        switch routerStyle{
        case .Push:
            controller.hidesBottomBarWhenPushed = true
            UIApplication.shared.currentViewController()?.navigationController?.pushViewController(controller, animated: true)
        case .Present:
            let navigationController = UINavigationController(rootViewController: controller)
            UIApplication.shared.currentViewController()?.present(navigationController, animated: true)
        }
    }
    
    static func routerURL(_ url:URL){
        var path = url.path.lowercased()
        if path.hasPrefix("/"){
            path = path.subString(from: 1)
        }
        guard let pageClass = SchemaConfig[path] else {
            return
        }
        self.routerClass(pageClass, input: url.queryDic)
    }
    /// 如果方便管理可以采用文件的方式存储，这里为了降低加载文件的耗时操作，采用直接内存储存
    private static var SchemaConfig:[String:AnyClass] = [:
        //"xxx/xxx": PageClass.self
    ]
}
